package com.kuang.springclound.controller;

import com.kuang.springclound.pojo.SystemDept;
import com.kuang.springclound.service.DeptService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//提供Restfull服务
@RestController
public class DeptController {

    @Resource
    DeptService deptService;
    //获取一些配置得到具体的微服务
    @Resource
    DiscoveryClient client;

    @PostMapping("/dept/add")
    public boolean addDept(@RequestBody SystemDept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public SystemDept queryById(@PathVariable("id") Long id) {
        SystemDept systemDept = deptService.queryById(id);

        if (systemDept==null){
            throw new RuntimeException("id=>"+id+"不存在该用户，或者信息无法查到");
        }

        return systemDept;
    }

    @GetMapping("/dept/list")
    public List<SystemDept> queryAll() {
        return deptService.queryAll();
    }

    //注册进来的微服务获取一些消息
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery->services:" + services);

        //得到一个具体的微服务信息,通过具体的微服务id，也就是applycationName
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUND-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost());
            System.out.println(instance.getPort());
            System.out.println(instance.getUri());
            System.out.println(instance.getServiceId());
        }
        return this.client;
    }

}

